package com.cqc.wheelviewdemo01;

import android.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

import java.util.Arrays;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private WheelView wheelView;
    private Button btn1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        wheelView = (WheelView) findViewById(R.id.wheelView);

        wheelView.setOffset(3);//上面3个，下面3个,这个在setItems(...)前，否则个别手机出错
        String[] array = getResources().getStringArray(R.array.province);
        wheelView.setItems(Arrays.asList(array));

        wheelView.setSeletion(15);//默认显示第16个
        //作者重写了方法setBackgroundDrawable(...),所以这里设置无效
//        wheelView.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape));
//        wheelView.setBackgroundResource(R.drawable.shape);

        wheelView.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                super.onSelected(selectedIndex, item);
                Log.d(TAG, "selectedIndex=" + selectedIndex + "----item" + item);
            }
        });

        btn1 = (Button) findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog();
            }
        });
    }

    private void showDialog() {
        View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog, null);
        WheelView wheelView = (WheelView) view.findViewById(R.id.wheelView_dialog);
        wheelView.setOffset(3);
        wheelView.setSeletion(15);
        String[] array = getResources().getStringArray(R.array.province);
        wheelView.setItems(Arrays.asList(array));
        wheelView.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                super.onSelected(selectedIndex, item);
                Log.d(TAG, "dialog--selectedIndex=" + selectedIndex + "----item" + item);
            }
        });

        new AlertDialog.Builder(MainActivity.this)
                .setTitle("请选择省份简称")
                .setView(view)
                .setPositiveButton("OK", null)
                .create()
                .show();
    }
}
